package com.luck.easyfoodbatch.pickerview;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.os.Handler;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.PopupWindow;

import com.luck.easyfoodbatch.R;
import com.luck.easyfoodbatch.util.Tools;

import java.util.List;


public class PopDateHelper {

    private Context context;
    private PopupWindow pop;
    private View view;
    private OnClickOkListener onClickOkListener;

    private List<String> listDate, ListSecond, ListPeriod, listTime,
            list24Time;
    private String date, time, stage, second;

    public PopDateHelper(Context context) {
        this.context = context;
        view = ((LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(
                R.layout.picker_date, null);
        pop = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT, true);
        initPop();
        initData();
        initView();
    }

    private void initPop() {
        pop.setAnimationStyle(android.R.style.Animation_InputMethod);
        pop.setFocusable(true);
        pop.setOutsideTouchable(true);
        pop.setBackgroundDrawable(new BitmapDrawable());
        pop.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
    }

    /**
     * 初始化数据
     */
    private void initData() {
        listDate = DatePackerUtil.getDateList();

        ListPeriod = DatePackerUtil.getPeriodAllList();
        listTime = DatePackerUtil.getTime12AllList();
        list24Time = DatePackerUtil.getTime24AllList();

        ListSecond = DatePackerUtil.getSecondAllList();

    }

    boolean b = false;
    boolean c = false;// 是否没有滚动 第一次选时间

    private void initView() {
        Button btnCancel = (Button) view.findViewById(R.id.btnCancel);
        Button btnOk = (Button) view.findViewById(R.id.btnOK);
        final LoopView loopView1 = (LoopView) view.findViewById(R.id.loopView1);
        final LoopView loopView2 = (LoopView) view.findViewById(R.id.loopView2);
        final LoopView loopView3 = (LoopView) view.findViewById(R.id.loopView3);

        final LoopView loopView4 = (LoopView) view.findViewById(R.id.loopView4);

        loopView1.setIsViewYear(false);// 不显示年
        loopView1.setList(listDate);
        loopView1.setNotLoop();
        loopView1.setCurrentItem(0);

        loopView2.setList(ListPeriod);
        loopView2.setNotLoop();
        loopView2.setCurrentItem(0);

        loopView3.setList(listTime);
        loopView3.setNotLoop();
        loopView3.setCurrentItem(0);

        loopView4.setList(ListSecond);
        loopView4.setNotLoop();
        loopView4.setCurrentItem(0);

        loopView1.setListener(new LoopListener() {
            @Override
            public void onItemSelect(int item) {
                if (!c) {
                    item = 0;
                }
                c = true;
                String select_item = listDate.get(item);
                date = select_item.substring(0, select_item.indexOf("日"))
                        .replace("年", "-").replace("月", "-");
            }
        });

        loopView2.setListener(new LoopListener() {
            @Override
            public void onItemSelect(int item) {
                stage = ListPeriod.get(item);

                if (b == false) {
                    loopView3.setList(listTime);
                    loopView3.setNotLoop();
                    loopView3.setCurrentItem(0);
                    b = true;
                } else {
                    if (stage.equals("上午")) {
                        loopView3.setList(listTime);
                        loopView3.setNotLoop();
                        loopView3.setCurrentItem(0);
                    } else {
                        loopView3.setList(list24Time);
                        loopView3.setNotLoop();
                        loopView3.setCurrentItem(0);
                    }
                }
                System.out.println("b:" + b);
            }
        });

        loopView3.setListener(new LoopListener() {
            @Override
            public void onItemSelect(int item) {
                if (Tools.isNull(stage)) {
                    stage = "上午";
                }
                if (stage.equals("上午")) {
                    time = listTime.get(item);
                } else {
                    time = list24Time.get(item);
                }

            }
        });

        loopView4.setListener(new LoopListener() {
            @Override
            public void onItemSelect(int item) {
                second = ListSecond.get(item);
            }
        });

        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pop.dismiss();
            }
        });

        btnOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pop.dismiss();
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        onClickOkListener.onClickOk(date, stage, time, second);
                    }
                }, 500);
            }
        });
    }

    /**
     * 显示
     *
     * @param view
     */
    public void show(View view) {
        pop.showAtLocation(view, Gravity.BOTTOM, 0, 0);
    }

    /**
     * 隐藏监听
     *
     * @param onDismissListener
     */
    public void setOnDismissListener(
            PopupWindow.OnDismissListener onDismissListener) {
        pop.setOnDismissListener(onDismissListener);
    }

    public void setOnClickOkListener(OnClickOkListener onClickOkListener) {
        this.onClickOkListener = onClickOkListener;
    }

    public interface OnClickOkListener {
        public void onClickOk(String date, String stage, String time,
                              String second);
    }

}
